import {
	createRouter,
	createWebHistory,
	createWebHashHistory
} from 'vue-router'
import {
	useUserStore
} from '@/stores/user.js'
import {
	showToast
} from 'vant';

import homeRoute from './home.js'
import userRoute from './user.js'
import goodsRoute from './goods.js'
const router = createRouter({
	history: createWebHashHistory(import.meta.env.BASE_URL),
	routes: [
		...homeRoute,
		...userRoute,
		...goodsRoute,
		{
			path: '/addressList',
			name: 'addressList',
			component: () => import('@/views/address/list.vue'),
			meta: {
				cache: false
			}
		},
	],
});
router.beforeEach((to,from,next)=>{
	// 离开了A页面 准备去B页面时  这个时机  beforeEach会执行
	const {isLogin} = useUserStore()
	// to.meta.auth   判别 哪些页面需要权限认证 
	if(to.meta.auth&&!isLogin){
		showToast('请先登陆')
		setTimeout(v=>{
			router.push('/login')
		},800)
	}else{
		next()
	}
	
})




export default router